package cn.buddha.jesj.thread.jmm;

import java.util.concurrent.CountDownLatch;

/**
 * @author shihao
 * @description 可见性
 */
public class Visibility {

    private static int threadSize = 10000;

    public static void main(String[] args) throws InterruptedException {

        VisibilityExample example = new VisibilityExample();
        CountDownLatch latch = new CountDownLatch(threadSize);

        for (int i = 0; i < threadSize; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        example.add();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        latch.countDown();
                    }
                }
            }).start();
        }

        latch.await();

        System.err.println(example.get());
    }

}

class VisibilityExample {
    private int a = 0;

    public void add() {
        a++;
    }

    public int get() {
        return a;
    }
}